package com.xckj.teacher.ui.activity

import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.xckj.teacher.R
import com.xckj.teacher.databinding.ActivityChangePhoneBinding
import com.xckj.teacher.framework.BaseActivity
import com.xckj.teacher.presenter.LoginPresenter
import com.xckj.teacher.presenter.MinePresenter
import com.xckj.teacher.util.CacheUtil
import com.xckj.teacher.util.CornerTransform
import com.xckj.teacher.util.SettingUtil


class ChangePhoneActivity : BaseActivity<ActivityChangePhoneBinding>() {

    var captchaToken = ""
    private val presenter: MinePresenter by lazy { MinePresenter(this) }
    private val loginPresenter: LoginPresenter by lazy { LoginPresenter(this) }

    override fun setStatusBarColor(): Int {
        return R.color.white
    }

    inner class ProxyClick {

        fun back() {
            finish()
        }

        fun change() {
            var account = dataBinding.etAccount.text.trim().toString()
            var code = dataBinding.etCode.text.trim().toString()
            var phoneCode = dataBinding.etPhoneCode.text.trim().toString()
            if (account.isNullOrEmpty()) {
                ToastUtils.showShort("请输入手机号")
                return
            }
            if (code.isNullOrEmpty()) {
                ToastUtils.showShort("请输入图形验证码")
                return
            }
            if (phoneCode.isNullOrEmpty()) {
                ToastUtils.showShort("请输入短信验证码")
                return
            }

            presenter.changeMobile(account, phoneCode, captchaToken, code) {
                CacheUtil.setToken("")
                CacheUtil.setUser(null)
                ActivityUtils.finishAllActivities()
                ActivityUtils.startActivity(LoginActivity::class.java)
            }
        }

        fun send() {
            var account = dataBinding.etAccount.text.trim().toString()
            if (account.isNullOrEmpty()) {
                ToastUtils.showShort("请输入手机号")
                return
            }
            presenter.sendSms(account, "changemobile") {

            }
        }

        fun reset() {
            captcha()
        }


    }

    override fun loadLayout(): Int {
        return R.layout.activity_change_phone
    }

    override fun initData() {
        dataBinding.presenter = presenter
        dataBinding.click = ProxyClick()
        captcha()
    }

    private fun captcha() {
        loginPresenter.getCaptcha { ra, captchaByte ->
            captchaToken = ra
            val transformation = CornerTransform(
                this,
                SettingUtil.dp2px(6f).toFloat()
            )
            transformation.setExceptCorner(
                leftTop = true,
                rightTop = false,
                leftBottom = true,
                rightBottom = false
            )

            Glide.with(this).load(captchaByte)
                .apply(RequestOptions.bitmapTransform(transformation))
                .into(dataBinding.ivCaptcha)

        }
    }

    override fun setListener() {

    }


    override fun isStatusBarDarkFont(): Boolean {
        return true
    }

}